﻿/*
BF3 RCON Simulator simulates the RCON interface of a BF3 server.
Copyright (C) 2011 Timi

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

You can contact us at http://bf3sim.codeplex.com/.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BF3RconSimulator
{
    //was put in here to allow VarsData's indexer to return success bool and (words on success)
    //will probably be used elsewhere
    class Response
    {
        internal bool IsSuccess { get; private set; } //TODO: see if this is necessary; easy to remove if not
        internal IEnumerable<string> Words { get; private set; }

        //ok will be included if not already there
        internal static Response Success(params string[] words)
        {
            //add OK if not already there
            if (words == null || words.Length == 0)
                //more efficient to do this rather than the method below
                words = new string[] { "OK" };
            else if (words[0] == "OK")
            {
                string[] newWords = new string[words.Length + 1];
                newWords[0] = "OK";

                for (int i = 0; i < words.Length; i++) newWords[i + 1] = words[i];

                return new Response(true, newWords);
            }

            return new Response(true, words);
        }

        internal static Response Fail(string error)
        {
            return new Response(false, new string[] { error });
        }

        internal static Response Fail()
        {
            return Response.Fail("UnknownCommand"); //is done auto by rconserver, but no need to put logic to check for words; assume there always words
        }

        private Response(bool isSuccess, IEnumerable<string> words)
        {
            IsSuccess = isSuccess;
            Words = words;
        }
    }
}
